﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;


namespace YFrameWork.RunTime
{
    /// <summary>
    /// 语言管理器
    /// </summary>
    public class LanguageComponent : YFrameWorkComponent
    {
        private MultiLanguageData mLanguageData;
        private MultiLanguageUnit mCurCountryUnit;
        private LanguageCountryType mCurCountryType;
        private Dictionary<LanguageTextType, MultiLanguageUnitArea> mCurTextDic;

        private Hashtable mLocalLanguage = new Hashtable
        {
            { "japan",LanguageCountryType.日文},
            { "korea",LanguageCountryType.韩文},
            { "india",LanguageCountryType.印度文},
            { "russia",LanguageCountryType.俄文},
            { "pt",LanguageCountryType.巴西文},
            { "fr",LanguageCountryType.法文},
        };
        
        /// <summary>
        /// 根据语言获取当前国家
        /// </summary>
        public string CountryAbbreviation
        {
            get
            {
                foreach (DictionaryEntry item in mLocalLanguage)
                {
                    if ((LanguageCountryType)item.Value == mCurCountryType)
                    {
                        return (string)item.Key;
                    }
                }
                return "english";
            }
        }
        private void Awake()
        {
            mCurTextDic = new Dictionary<LanguageTextType, MultiLanguageUnitArea>();
            mLanguageData = GameEntry.Instance.GetComponent<ResourceComponent>().LoadResource<MultiLanguageData>("Language/MultiLanguageData");
            JudgeCurrentCountry();
   
        }

        private void JudgeCurrentCountry()
        {
            string languageString;
#if UNITY_EDITOR
            languageString = "zh-Hans";
#elif UNITY_ANDROID
            AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            AndroidJavaObject _unityContext = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
            AndroidJavaObject locale = _unityContext.Call<AndroidJavaObject>("getResources").Call<AndroidJavaObject>("getConfiguration").Get<AndroidJavaObject>("locale");
            languageString = locale.Call<string>("getLanguage");
#elif UNITY_IOS
            languageString = getPreferredLanguage();
#endif
            Debug.Log(languageString);
            string wholeString = languageString;
            languageString = languageString.Substring(0, languageString.IndexOf('-') <= 0 ? languageString.Length : languageString.IndexOf('-'));
            if (mLocalLanguage.Contains(languageString)) mCurCountryType = (LanguageCountryType)mLocalLanguage[languageString];
            else mCurCountryType = LanguageCountryType.英文;
            if (wholeString.Contains("zh-Hans") || wholeString.Contains("zh-Hant"))
                mCurCountryType = LanguageCountryType.繁体中文;
            ChangeFont(mCurCountryType);
         
            
        }

        /// <summary>
        /// 获取指定文字信息
        /// </summary>
        /// <param name="textType"></param>
        /// <returns></returns>
        public string GetTextContent(LanguageTextType textType)
        {
            if (mCurTextDic.TryGetValue(textType,out MultiLanguageUnitArea area) && !string.IsNullOrEmpty(area.value))
            {
                return area.value;
            }
            else
            {
                throw new GameFrameworkException("没有获取到指定的文字");
            }
        }

        /// <summary>
        /// 改变国家
        /// </summary>
        public void ChangeFont(LanguageCountryType countryType)
        {
            mCurCountryType = countryType;
           MultiLanguageUnit ? multi = GetCurCountryData(mCurCountryType);
            if (multi == null)
            {
                throw new GameFrameworkException("当前解析的国家在字典内不存在！");
            }
            mCurCountryUnit = (MultiLanguageUnit)multi;
            mCurTextDic.Clear();
            foreach (var item in mCurCountryUnit.value_allAreas)
            {
                mCurTextDic.Add(item.area, item);
            }
            GameEntry.Instance.GetComponent<EventComponent>().Fire(this, ChangeLanguageArgs.Create());
        }



        private MultiLanguageUnit? GetCurCountryData(LanguageCountryType countryType)
        {
            foreach (var item in mLanguageData.languageUnits)
            {
                if (item.country == countryType)
                {
                   
                    return item;
                }
            }
            return null;
        }

        /// <summary>
        /// 获取不同国家资金倍数
        /// </summary>
        /// <param name="textType"></param>
        /// <returns></returns>
        public int GetCountryMoneyMultiple()
        {
            try
            {
                return int.Parse(System.Text.RegularExpressions.Regex.Replace(mCurTextDic[LanguageTextType.MoneyMultiple].value, @"[^0-9]+", ""));
            }
            catch (System.Exception)
            {

                throw new GameFrameworkException("没有获取到这个国家的资金倍数！");
            }
        }


    }
    /// <summary>
    /// 切换语言的事件
    /// </summary>
    public class ChangeLanguageArgs : YFrameWorkEventArgs
    {
        public override int Id => EventID;
        public static readonly int EventID = typeof(ChangeLanguageArgs).GetHashCode();

        public static ChangeLanguageArgs Create()
        {
            ChangeLanguageArgs args = ReferencePool.Acquire<ChangeLanguageArgs>();
            return args;
        }
        public override void Clear()
        {
            
        }
    }
}